<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Group\Models;

use IKPHP\Common\Models\BaseModel;

class GroupCate extends BaseModel {
	
	public $cateid;
	public $catename;	
	
	public function getSource() {
		return IK . "group_cate";
	}
	/**
	 * 获取缓存的分类
	 * */
	public static  function getCates(){
        $cacheKey = 'group-cate';
        $cates= self::$modelCache->get($cacheKey);  
        if($cates == null){
			$res = self::find(array(
				'conditions'=> "referid = 0",
				'order'	=> 'orderid DESC',
			));

            self::$modelCache->save($cacheKey, $res);
            $cates = $res; 
        }
        return $cates->toArray();	
	} 
	/**
	 * 根据上级ID获取子类
	 * */
	public static function getReferCate($referid) {
		$result = array();
		$result = self::find(array(
			'conditions'=> "referid = '$referid'",
			'order'	=> 'orderid DESC',
		));
		if(count($result)>0){
			return $result->toArray();
		}
		return $result;
	}
	/**
	 * 根据ID或者别名获取分类
	 * */
	public static function getOneCate($cateid){
		$result = array();
		if(is_numeric($cateid)){
			$result = self::findFirst(array(
				'conditions'=> "cateid = '$cateid'",
			));
		}elseif(is_string($cateid)){
			$result = self::findFirst(array(
				'conditions'=> "alias = '$cateid'",
			));
		}
		if($result){
			return $result->toArray();
		}
		return $result;
	}	
}
